import plotly.express as px import plotly.io as pio
pio.renderers.default = "notebook"
From the course website:
url = "https://raw.githubusercontent.com/plotly/datasets/master/2011_us_ag_exports.csv"
import pandas as pd
df = pd.read_csv(url)
df.head(5)
| code | state | category | total exports | beef | pork | poultry | dairy | fruits fresh | fruits proc | total fruits | veggies fresh | veggies proc | total veggies | corn | wheat | cotton | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | AL | Alabama | state | 1390.63 | 34.4 | 10.6 | 481.0 | 4.06 | 8.0 | 17.1 | 25.11 | 5.5 | 8.9 | 14.33 | 34.9 | 70.0 | 317.61 |
| 1 | AK | Alaska | state | 13.31 | 0.2 | 0.1 | 0.0 | 0.19 | 0.0 | 0.0 | 0.00 | 0.6 | 1.0 | 1.56 | 0.0 | 0.0 | 0.00 |
| 2 | AZ | Arizona | state | 1463.17 | 71.3 | 17.9 | 0.0 | 105.48 | 19.3 | 41.0 | 60.27 | 147.5 | 239.4 | 386.91 | 7.3 | 48.7 | 423.95 |
| 3 | AR | Arkansas | state | 3586.02 | 53.2 | 29.4 | 562.9 | 3.53 | 2.2 | 4.7 | 6.88 | 4.4 | 7.1 | 11.45 | 69.5 | 114.5 | 665.44 |
| 4 | CA | California | state | 16472.88 | 228.7 | 11.1 | 225.4 | 929.95 | 2791.8 | 5944.6 | 8736.40 | 803.2 | 1303.5 | 2106.79 | 34.6 | 249.3 | 1064.95 |
df["exports_cotton"] = df["cotton"] > 0
df.head()
| code | state | category | total exports | beef | pork | poultry | dairy | fruits fresh | fruits proc | total fruits | veggies fresh | veggies proc | total veggies | corn | wheat | cotton | exports_cotton | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | AL | Alabama | state | 1390.63 | 34.4 | 10.6 | 481.0 | 4.06 | 8.0 | 17.1 | 25.11 | 5.5 | 8.9 | 14.33 | 34.9 | 70.0 | 317.61 | True |
| 1 | AK | Alaska | state | 13.31 | 0.2 | 0.1 | 0.0 | 0.19 | 0.0 | 0.0 | 0.00 | 0.6 | 1.0 | 1.56 | 0.0 | 0.0 | 0.00 | False |
| 2 | AZ | Arizona | state | 1463.17 | 71.3 | 17.9 | 0.0 | 105.48 | 19.3 | 41.0 | 60.27 | 147.5 | 239.4 | 386.91 | 7.3 | 48.7 | 423.95 | True |
| 3 | AR | Arkansas | state | 3586.02 | 53.2 | 29.4 | 562.9 | 3.53 | 2.2 | 4.7 | 6.88 | 4.4 | 7.1 | 11.45 | 69.5 | 114.5 | 665.44 | True |
| 4 | CA | California | state | 16472.88 | 228.7 | 11.1 | 225.4 | 929.95 | 2791.8 | 5944.6 | 8736.40 | 803.2 | 1303.5 | 2106.79 | 34.6 | 249.3 | 1064.95 | True |
import plotly.express as px
fig = px.choropleth(df,
locationmode="USA-states",
locations="code",
color="exports_cotton",
scope="usa",
color_discrete_sequence = ["red", "lightgray"],
title = "States exporting cotton",
hover_name = "state",
hover_data={"exports_cotton": False, "code":False}
)
fig.update_layout(showlegend=False)
fig.show()
df["veggies_perc"] = (df["total veggies"]/df["total exports"])*100
df.head(5)
| code | state | category | total exports | beef | pork | poultry | dairy | fruits fresh | fruits proc | total fruits | veggies fresh | veggies proc | total veggies | corn | wheat | cotton | exports_cotton | veggies_perc | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | AL | Alabama | state | 1390.63 | 34.4 | 10.6 | 481.0 | 4.06 | 8.0 | 17.1 | 25.11 | 5.5 | 8.9 | 14.33 | 34.9 | 70.0 | 317.61 | True | 1.030468 |
| 1 | AK | Alaska | state | 13.31 | 0.2 | 0.1 | 0.0 | 0.19 | 0.0 | 0.0 | 0.00 | 0.6 | 1.0 | 1.56 | 0.0 | 0.0 | 0.00 | False | 11.720511 |
| 2 | AZ | Arizona | state | 1463.17 | 71.3 | 17.9 | 0.0 | 105.48 | 19.3 | 41.0 | 60.27 | 147.5 | 239.4 | 386.91 | 7.3 | 48.7 | 423.95 | True | 26.443270 |
| 3 | AR | Arkansas | state | 3586.02 | 53.2 | 29.4 | 562.9 | 3.53 | 2.2 | 4.7 | 6.88 | 4.4 | 7.1 | 11.45 | 69.5 | 114.5 | 665.44 | True | 0.319295 |
| 4 | CA | California | state | 16472.88 | 228.7 | 11.1 | 225.4 | 929.95 | 2791.8 | 5944.6 | 8736.40 | 803.2 | 1303.5 | 2106.79 | 34.6 | 249.3 | 1064.95 | True | 12.789445 |
fig = px.choropleth(df,
locationmode="USA-states",
scope="usa",
locations="code",
color="veggies_perc",
hover_name="state",
hover_data={"veggies_perc": ':.2f', "code": False},
labels={"veggies_perc": "veggies %"},
color_continuous_scale="tempo",
title="Percentage of vegetables in agricultural exports",
)
#fig.update_traces(marker_line_color='white',
# hovertemplate = '<b>%{hovertext}</b><br>Veggies exports: %{z:.2f}%<extra></extra>'
# )
fig.show()
fig.data[0].hovertemplate
'<b>%{hovertext}</b><br><br>veggies %=%{z:.2f}<extra></extra>'
dir(px.colors.sequential)[:10]
['Aggrnyl', 'Aggrnyl_r', 'Agsunset', 'Agsunset_r', 'Blackbody', 'Blackbody_r', 'Bluered', 'Bluered_r', 'Blues', 'Blues_r']
import requests
r = requests.get("https://mth548.org")
r.status_code
200
r.headers
{'Connection': 'keep-alive', 'Content-Length': '5719', 'Server': 'GitHub.com', 'Content-Type': 'text/html; charset=utf-8', 'Last-Modified': 'Fri, 11 Mar 2022 04:34:43 GMT', 'Access-Control-Allow-Origin': '*', 'ETag': 'W/"622ad163-47c3"', 'expires': 'Sat, 12 Mar 2022 16:24:28 GMT', 'Cache-Control': 'max-age=600', 'Content-Encoding': 'gzip', 'x-proxy-cache': 'MISS', 'X-GitHub-Request-Id': '3B3A:07C6:124F1BA:1F3B18C:622CC6E4', 'Accept-Ranges': 'bytes', 'Date': 'Sat, 12 Mar 2022 16:43:53 GMT', 'Via': '1.1 varnish', 'Age': '0', 'X-Served-By': 'cache-ewr18132-EWR', 'X-Cache': 'MISS', 'X-Cache-Hits': '0', 'X-Timer': 'S1647103433.029469,VS0,VE11', 'Vary': 'Accept-Encoding', 'X-Fastly-Request-ID': '1583173322e6ece26680c505fc8ca7966ab2ca3e'}
dict(r.headers)
{'Connection': 'keep-alive',
'Content-Length': '5719',
'Server': 'GitHub.com',
'Content-Type': 'text/html; charset=utf-8',
'Last-Modified': 'Fri, 11 Mar 2022 04:34:43 GMT',
'Access-Control-Allow-Origin': '*',
'ETag': 'W/"622ad163-47c3"',
'expires': 'Sat, 12 Mar 2022 16:24:28 GMT',
'Cache-Control': 'max-age=600',
'Content-Encoding': 'gzip',
'x-proxy-cache': 'MISS',
'X-GitHub-Request-Id': '3B3A:07C6:124F1BA:1F3B18C:622CC6E4',
'Accept-Ranges': 'bytes',
'Date': 'Sat, 12 Mar 2022 16:43:53 GMT',
'Via': '1.1 varnish',
'Age': '0',
'X-Served-By': 'cache-ewr18132-EWR',
'X-Cache': 'MISS',
'X-Cache-Hits': '0',
'X-Timer': 'S1647103433.029469,VS0,VE11',
'Vary': 'Accept-Encoding',
'X-Fastly-Request-ID': '1583173322e6ece26680c505fc8ca7966ab2ca3e'}
print(r.text[:1000])
<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
<meta charset="utf-8" />
<meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>MTH 448/548 Syllabus — MTH 448/548 documentation</title>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="_static/copybutton.css" type="text/css" />
<link rel="stylesheet" href="_static/css/custom.css" type="text/css" />
<link rel="shortcut icon" href="_static/favicon.ico"/>
<link rel="canonical" href="https://www.mth548.org/index.html" />
<!--[if lt IE 9]>
<script src="_static/js/html5shiv.min.js"></sc
r = requests.get("https://www.mth548.org/_static/report_rubrics.pdf")
with open("rubrics.pdf", 'wb') as foo:
foo.write(r.content)
ls
rubrics.pdf week_7_prep.ipynb week_7_class.ipynb week_7_prep_2020.ipynb
grads = requests.get("http://www.buffalo.edu/cas/math/people/grad-directory.html").text
grads[:1000]
'<!DOCTYPE HTML><html lang="en" class="ubcms-65"><!-- cmspub06 0312-121942 --><head><link rel="preconnect" href="https://www.googletagmanager.com/" crossorigin/><link rel="dns-prefetch" href="https://www.googletagmanager.com/"/><link rel="dns-prefetch" href="https://connect.facebook.net/"/><link rel="dns-prefetch" href="https://www.google-analytics.com/"/><meta http-equiv="X-UA-Compatible" content="IE=edge"/><meta http-equiv="content-type" content="text/html; charset=UTF-8"/><meta id="meta-viewport" name="viewport" content="width=device-width,initial-scale=1"/><script>if (screen.width > 720 && screen.width < 960) document.getElementById(\'meta-viewport\').setAttribute(\'content\',\'width=960\');</script><script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({\'gtm.start\':new Date().getTime(),event:\'gtm.js\'});var f=d.getElementsByTagName(s)[0],j=d.createElement(s),dl=l!=\'dataLayer\'?\'&l=\'+l:\'\';j.async=true;j.src=\'https://www.googletagmanager.com/gtm.js?id=\'+i+dl;f.parentNode.insertBefore(j,f);})(w'
%%HTML
<html>
<head>
<style>
.myclass {
color: magenta;
font-weight: 900;
background-color: lime;
}
</style>
</head>
</body>
<h1 style="color: red; font-size: 100px;">Hello !</h1>
<p>This is some text</p>
<p><em>This is another paragraph</em></p>
<p class="myclass"> Here is some <br/> text and <span style="color: green;">some colored text</span> and some more text.</p>
<p>This is the website of the <a href="http://www.math.buffalo.edu" title="UB Math" target="blank_">Math Department</a></p>
<h2 class="myclass">List</h2>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 2</li>
</ul>
<h2>Grade Table</h2>
<table>
<tr>
<th>Name</th>
<th>Exam Score</th>
<th>Grade</th>
</tr>
<tr>
<td>John</td><td>90</td><td>A</td>
</tr>
<tr>
<td>Mary</td><td>72</td><td>B-</td>
</tr>
<tr>
<td>Bob</td><td>60</td><td>C+</td>
</tr>
</table>
<img src="https://picsum.photos/200" alt="a picture" width="200" height="300">
</body>
</html>
This is some text
This is another paragraph
Here is some
text and some colored text and some more text.
This is the website of the Math Department
| Name | Exam Score | Grade |
|---|---|---|
| John | 90 | A |
| Mary | 72 | B- |
| Bob | 60 | C+ |